// pages/login.js
const app = getApp()
var CONFIG = require("../config/config.js");
var share = require("../utils/share.js");
Page({
    data: {
        motto: 'Hello World',
        userInfo: {},
        hasUserInfo: false,
        canIUse: wx.canIUse('button.open-type.getUserInfo'),
        time: '获取验证码', //倒计时 
        currentTime: 60,
        disabled: false,
        tel: '',
        tel1: '',
        code: '',
        phone: '',
        telfalse: false,
        loginfalse: false,
        type: 2,
        severfalse: false,
    },
    severclick() {
        var that = this;
        that.setData({
            severfalse: !that.data.severfalse
        })
    },
    butclose() {
        wx.navigateTo({
            url: '/pages/loginfirst',
        })
    },
    sever() {
        wx.navigateTo({
            url: '/pages/sever',
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        var phone = wx.getStorageSync('tel');
        // console.log(phone);
        if (phone != '') {
            that.setData({
                tel: phone.replace(phone.substr(3, 4), "****"),
                tel1: phone
            })
            that.sharelogin(that.data.tel1,1);
        } else {
            that.setData({
                tel1: ''
            })
        }

        if (options.type) {
            that.setData({
                type: options.type
            })
        }

        // 清除 搜索数据
        wx.removeStorage({
            key: 'parmdata',
            success: function () {
            },
            fail: function () {
                wx.showModal({
                    title: CONFIG.MODEL_TITLE,
                    showCancel: false,
                    content: CONFIG.REQUEST_FAIL,
                    confirmText: '点击重试',
                    success: function (res) {
                        wx.navigateBack();
                    }
                })
            }
        })
        wx.removeStorage({
            key: 'prma',
            success: function () {
            },
            fail: function () {
                wx.showModal({
                    title: CONFIG.MODEL_TITLE,
                    showCancel: false,
                    content: CONFIG.REQUEST_FAIL,
                    confirmText: '点击重试',
                    success: function (res) {
                        wx.navigateBack();
                    }
                })
            }
        })

        
    },
    // 底部
    natbottom() {
        var phone = wx.getStorageSync('tel');
        var obj = {
            phone: phone,
            token: 1,
        }
        share.wxRequest('crmapi/user/frontMenuList', obj, function (res) {
            if (Number(res.data.errcode) == 0) {
                var data = res.data.data;
                wx.setStorageSync('navarr', JSON.stringify(data));
                wx.hideLoading()
                var item = data[0];
                // console.log(data);
                // console.log(item);
                if (item.name == 'crm'){
                    wx.navigateTo({
                        url: '../../staff/mycustom'
                    })
                } else if (item.name == 'apply'){
                    wx.navigateTo({
                        url: '/pages/apply/index'
                    })
                } else if (item.name == 'my') {
                    wx.navigateTo({
                        url: '/pages/staff/my'
                    })
                }
            }
        })
    },

    onShow: function () {
        wx.showLoading({
            title: '登录中~',
        })
        var that = this;
        var phone = wx.getStorageSync('tel');
        // console.log(phone);
        if (phone != '') {
            that.setData({
                tel: phone.replace(phone.substr(3, 4), "****"),
                tel1: phone
            })
            that.sharelogin(that.data.tel1,1);
        } else {
            that.setData({
                tel1: ''
            })
            wx.hideLoading();
        }
    },


    // 用户点击右上角分享
    onShareAppMessage: function () {
        return {
            title: CONFIG.SHARETITLE,
            path: CONFIG.PATHLOGIN
        }
    },

    // 下拉刷新
    onPullDownRefresh: function () {
        // 登录
        // this.sharelogin(this.data.tel1);
        wx.stopPullDownRefresh();
    },



    // 登录
    sharelogin: function (tel,type) {
        var that = this;
        var data = {
            phone: tel,
            token: '12345'
        }
        share.wxRequest('crmapi/User/isAdmin',data,function(res){
            if (Number(res.data.errcode) == 0) {
                var data = res.data.data;
                var role = res.data.role;
                // "admin_id": 0,//0:学员  1:内部员工
                // "consult_id": 0//0:学员  1:课程顾问ID
                app.globalData.admin_id = data.admin_id;
                app.globalData.uid = data.uid;
                // "is_stu": 1,//是否是学员 0:不是 1:是
                //     "is_cons": 0,//是否是课程顾问 0:不是 1::顾问 2:代理
                //         "is_team": 0,//是否是校区管理员 0:不是 1:是
                //             "is_depart": 0,//是否是部门管理员 0:不是 1:是
                //                 "is_teacher": 0//是否老师 0:不是 1:是
                if (Number(role.is_stu) == 1){
                    var identity = 1;
                    var is_dl = 0;
                }else{
                    if (Number(role.is_learn)==1){
                        var identity = 3;
                        var is_dl = 0;
                    }else{
                        var is_dl = 0;
                        if(Number(role.is_cons)==2){
                            var is_dl = 1;
                        }
                        var identity = 2;
                    }
                }
                // "identity": 1//1:学员 2：课程顾问 3学管师
                app.globalData.identity = identity;
                app.globalData.is_dl = is_dl;
                // console.log(app.globalData.identity)
                app.globalData.tel = tel;
                app.globalData.role = role;
                wx.setStorageSync('tel', tel);
                that.setData({
                    tel: tel.replace(tel.substr(3, 4), "****"),
                    tel1: tel,
                    loginfalse: true
                })
                if (Number(type == 2)) {
                    //   console.log(898)
                    wx.setStorageSync('tel', tel);
                    setTimeout(function () {
                        
                        if (Number(identity) == 2 || Number(identity) == 3) {
                            // 课程顾问
                            
                            that.natbottom();
                        } else {
                            wx.navigateTo({
                                url: 'public/userCourses?user_id=' + app.globalData.uid + ''
                            })
                        }
                    }, 300)
                }
            } else {
                that.setData({
                    loginfalse: false
                })
                wx.showToast({
                    title: res.data.msg,
                    icon: 'none'
                })
            }
            wx.hideLoading();
            
        },function(e){
            wx.hideLoading();
            that.setData({
                loginfalse: false
            })
            wx.showModal({
                title: CONFIG.MODEL_TITLE,
                showCancel: false,
                content: CONFIG.REQUEST_FAIL,
                confirmText: '点击重试',
                success: function (res) {
                    // wx.navigateBack();
                }
            })
        })
    },

    // 点击进入
    gotoIndex: function () {
        var that = this;
        if (that.data.tel1 == '') {
            wx.showModal({
                title: CONFIG.MODEL_TITLE,
                showCancel: false,
                content: CONFIG.REQUEST_FAIL,
                confirmText: '点击重试',
                success: function (res) {
                    wx.navigateBack();
                }
            })
        } else {
            // console.log(that.data.loginfalse)
            if (that.data.loginfalse) {
                var identity = app.globalData.identity;
                if (Number(identity) == 2 || Number(identity) == 3) {
                    // 课程顾问
                    wx.showLoading({
                        title: '加载中~',
                    })
                    that.natbottom();
                } else {
                    wx.navigateTo({
                        url: 'public/userCourses?user_id=' + app.globalData.uid + ''
                    })
                }
            } else {
                wx.showModal({
                    title: CONFIG.MODEL_TITLE,
                    showCancel: false,
                    content: CONFIG.REQUEST_FAIL,
                    confirmText: '点击重试',
                    success: function (res) {
                        wx.navigateBack();
                    }
                })
            }
        }
    },

    // 手机号失去焦点
    phoneblur: function (e) {
        this.setData({
            phone: e.detail.value
        })
    },

    messfocus: function (e) {
        // console.log(e);
        this.setData({
            phone: e.detail.value
        })
    },

    // 点击发送验证码
    messageClick: function (e) {
        var that = this;
        var subfalse = that.data.severfalse;
        if (subfalse) {
            var currentTime = that.data.currentTime;
            var phone = that.data.phone;
            // console.log(phone);
            if (phone != '') {
                if ((/^1(3|4|5|6|7|8|9)\d{9}$/.test(phone))) {
                    var time = setInterval(function () {
                        that.setData({
                            time: currentTime + 's',
                            disabled: true
                        })
                        currentTime--;
                        if (currentTime <= 0) {
                            clearInterval(time);
                            that.setData({
                                time: '发送验证码',
                                currentTime: 60,
                                disabled: false
                            })
                        }
                    }, 1000)
                    var data = {
                        tel: phone,
                        from: 1,
                    }
                    share.wxRequest('crmapi/User/smsCode',data,function(res){
                            // console.log(res);
                            if (Number(res.data.errcode) == 0) {
                                wx.showToast({
                                    title: '发送成功',
                                    icon: 'none',
                                    duration: 1500
                                })
                            } else {
                                wx.showToast({
                                    title: res.data.msg,
                                    icon: 'none',
                                    duration: 1500
                                })
                            }
                    })
                } else {
                    wx.showToast({
                        title: '请输入正确手机号',
                        icon: 'none',
                        duration: 1500
                    })
                }
            } else {
                wx.showToast({
                    title: '请输入您的手机号',
                    icon: 'none',
                    duration: 1500
                })
            }
        } else {
            wx.showToast({
                title: '请先阅读并同意《服务协议》',
                icon: 'none',
                duration: 1500
            })
        }
    },

    // 点击提交
    loginSubmit: function (e) {
        // console.log(e);
        var that = this;
        var subfalse = that.data.severfalse;
        if (subfalse) {
            var e1 = e.detail.value;
            // console.log(e1);
            if (e1.phone == '') {
                wx.showToast({
                    title: '请输入您的手机号',
                    icon: 'none',
                    duration: 1500
                })
            } else if (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(e1.phone))) {
                wx.showToast({
                    title: '请输入正确的手机号',
                    icon: 'none',
                    duration: 1500
                })
            } else if (Number(e1.message.length) != 4) {
                wx.showToast({
                    title: '请输入4位的验证码',
                    icon: 'none',
                    duration: 1500
                })
            }else if (e1.message == '') {
                wx.showToast({
                    title: '请输入验证码',
                    icon: 'none',
                    duration: 1500
                })
            } else {
                wx.showLoading({
                    title: '提交中~',
                })
                var data = {
                    phone: e1.phone,
                    token: '12345',
                    code: e1.message
                }
                share.wxRequest('crmapi/User/login',data,function(res){
                    // console.log(res);
                    if (Number(res.data.errcode) == 0) {
                        wx.hideLoading();
                        wx.showToast({
                            title: '提交成功',
                            icon: 'none',
                            duration: 1500
                        })
                        that.sharelogin(e1.phone, 2);
                        
                    } else {
                        wx.hideLoading();
                        wx.showToast({
                            title: res.data.msg,
                            icon: 'none',
                            duration: 1500
                        })
                    }
                })
            }
        } else {
            wx.showToast({
                title: '请先阅读并同意《服务协议》',
                icon: 'none',
                duration: 1500
            })
        }
    }
})